<template>
  <el-form :model="form" ref="form" :rules="rules" label-width="110px" label-position="left">
    <el-form-item label="原密码" prop="prevPassword">
      <el-input v-model="form.prevPassword" size="medium" type="password" class="small-input"></el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="password">
      <el-input v-model="form.password" size="medium" type="password" class="small-input"></el-input>
      <span class="password-tips">输入6-12位密码，包含数字和字母；新密码不能和原密码相同</span>
    </el-form-item>
    <el-form-item label="确认新密码" prop="comfirmPassword">
      <el-input v-model="form.comfirmPassword" size="medium" type="password" class="small-input"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary">完成</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  // 全局感知
  name: "PassowrdChange",

  // 模板依赖
  components: {},
  filters: {},

  // 接口
  props: {},

  // 本地状态
  data () {
    const checkpass0 = (rule, value, callback) => {
      if (!value) callback(new Error('请输入原密码'))
      else if (this.form.password) this.$refs.form.validateField('password')
      else callback()
    }

    const checkpass1 = (rule, value, callback) => {
      if (!value) callback(new Error('请输入新密码'))
      else if (value === this.form.prevPassword) callback(new Error('新密码不能和原密码相同!'))
      else if (this.form.comfirmPassword) this.$refs.form.validateField('comfirmPassword')
      else callback()
    }

    const checkpass2 = (rule, value, callback) => {
      if (!value) callback(new Error('请再次输入新密码'))
      else if (value !== this.form.password) callback(new Error('两次输入密码不一致!'))
      else callback()
    }

    return {
      form: {
        prevPassword: 123456789,
        password: 987654321,
        comfirmPassword: 987654321
      },
      rules: {
        prevPassword: [{ validator: checkpass0, trigger: 'blur' }],
        password: [{ validator: checkpass1, trigger: 'blur' }],
        comfirmPassword: [{ validator: checkpass2, trigger: 'blur' }],
      }
    };
  },
  computed: {},

  // 事件
  watch: {},
  beforeCreate () { },
  created () { },
  beforeMount () { },
  mounted () { },
  beforeUpdate () { },
  updated () { },
  beforeDestroy () { },
  destroyed () { },

  // 非响应式的属性
  methods: {}
}
</script>

<style lang="less" scoped>
.small-input {
  width: 280px;
}
.password-tips {
  margin-left: 10px;

  color: #666;

  font-size: 12px;
}
</style>